<template>
  <div class="container">
    <div class="basepadding">设备管理</div>
    <div class="basepadding">
      <div class="basepadding" style="background: rgba(255, 255, 255, 0.1)">
        <z-from
          ref="searchfrom"
          :inline="true"
          label-position="left"
          label-width="100px"
          :nowinputs="ChargeCarToPcssearchinputs"
          size="small"
        >
          <template #zfromafter>
            <el-form-item style="margin: 0">
              <el-button type="primary" @click="searchfn()">搜索</el-button>
              <el-button @click="deletesearchfn()">清空</el-button>
            </el-form-item>
          </template>
        </z-from>
      </div>
    </div>
    <div class="basepadding" style="text-align: right">
      <el-button type="primary" @click="ChargeCarToPcsaddfn()">添加</el-button>
    </div>
    <div class="basepadding">
      <z-table
        v-loading="tableloading"
        :datas="ChargeCarToPcs"
        :tablecolumn="tablecolumn"
        @delfn="delfn"
      >
        <template #setting="setting">
          <el-table-column
            align="center"
            :label="setting.itemdata.label"
            :prop="setting.itemdata.value"
          >
            <template #default="scope">
              <el-button
                size="small"
                type="primary"
                @click="ChargeCarToPcsaddfn(scope.row)"
              >
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </z-table>
    </div>

    <el-dialog
      v-model="dialogfalg"
      :close-on-click-modal="false"
      :title="dialogtitle"
      width="800px"
      @closed="closedfn"
      @opened="openfn"
    >
      <z-from
        v-if="dialogfalg"
        ref="formz"
        colspan="12"
        label-position="left"
        label-width="100px"
        :nowdata="nowdata"
        :nowinputs="nowinputs"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogfalg = false">取 消</el-button>
          <a
            href="../../exels/设备管理.xls"
            style="margin: 0 10px"
            target="_blank"
          >
            <el-button type="primary">导出excel</el-button>
          </a>
          <el-button
            :loading="edtbtnloading"
            type="primary"
            @click="updatafn()"
          >
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
  import {
    ChargeCarToPcsaddinputs,
    ChargeCarToPcssearchinputs,
    tablecolumn,
  } from './json.js'
  import zFrom from '../../components/zFrom/zFrom'
  import zTable from '../../components/zTable/index'

  export default {
    name: 'ChargeCarToPcs',
    components: {
      zFrom,
      zTable,
    },
    data() {
      return {
        ChargeCarselect: {},
        Stationselect: {},

        dialogfalg: false,
        edtbtnloading: false,
        nowdata: {},
        nowinputs: {},
        dialogtitle: '',
        ChargeCarToPcs: [],
        updatas: [],
        tablecolumn,
        tableloading: false,
        ChargeCarToPcssearchinputs,
      }
    },
    mounted() {
      this.getdata()
    },
    methods: {
      searchfn() {
        this.$refs.searchfrom.validate().then((updata) => {
          this.ChargeCarToPcs = this.updatas.filter((e) => {
            for (let i = 0; i < this.ChargeCarToPcssearchinputs.length; i++) {
              let dqinput = this.ChargeCarToPcssearchinputs[i],
                v =
                  e[dqinput.value] || e[dqinput.value] == 0
                    ? e[dqinput.value]
                    : ''
              if (dqinput.type == 'select') {
                if (
                  updata[dqinput.value] &&
                  updata[dqinput.value] != '' &&
                  updata[dqinput.value] != v
                ) {
                  return false
                }
              } else {
                if (
                  updata[dqinput.value] &&
                  updata[dqinput.value] != '' &&
                  v.indexOf(updata[dqinput.value]) < 0
                ) {
                  return false
                }
              }
            }
            return true
          })
        })
      },
      deletesearchfn() {
        this.searchdata = {}
        this.$refs.searchfrom.settingfn()
        this.ChargeCarToPcs = this.$z.deepClone(this.updatas)
      },
      async getdata() {
        this.tableloading = true
        let data = await this.$z.$axios.post('/ChargeCarToPcs')
        let ChargeCar = await this.$z.$axios.post('/ChargeCar')
        let Station = await this.$z.$axios.post('/Station')
        this.ChargeCarselect = {}
        ChargeCar.data.forEach((e) => {
          this.ChargeCarselect[e.gtel] = e
        })
        this.Stationselect = {}
        Station.data.forEach((e) => {
          this.Stationselect[e.id] = e
        })
        let tabledata = data.data.map((e) => {
          return {
            ...e,
            pcsmc: this.Stationselect[e.pcs]
              ? this.Stationselect[e.pcs].pcs
              : '',
            mc: this.ChargeCarselect[e.gtel]
              ? this.ChargeCarselect[e.gtel].mc
              : '',
          }
        })

        console.log(tabledata, 'datadatadatadata')
        this.updatas = tabledata
        this.ChargeCarToPcs = tabledata
        this.tableloading = false
      },
      ChargeCarToPcsaddfn(scope = {}) {
        let data = scope[Symbol.for('data')]
        this.dialogfalg = true
        this.dialogtitle = data ? '修改' : '添加'
        let btns = data ? ChargeCarToPcsedtinputs : ChargeCarToPcsaddinputs
        this.nowinputs = [...btns]
        this.nowdata = data ? data : {}
      },
      openfn() {},
      closedfn() {},
      async updatafn() {
        let updata = await this.$refs.formz.validate()
        let reqdata = { pcs: updata.pcs }
        updata.excel.forEach(function (e, i) {
          reqdata[`gtel${i + 1}`] = e.gtels
        })
        reqdata.sum = updata.excel.length
        this.edtbtnloading = true
        let url = '/ChargeCarToPcsadd'
        let { data } = await this.$z.$axios.post(url, reqdata).catch(() => {
          this.edtbtnloading = false
        })
        let par = this.$z.parfn(data)
        if (par.value == 'succeed') {
          this.$message.success(par.label)
          this.edtdialogfalg = false
          this.getdata()
        } else {
          this.$message.error(par.label)
        }
        this.edtbtnloading = false
        this.dialogfalg = false
      },
      async gtelblur(gtel) {
        if (!gtel || gtel == '') {
          return
        }
        let { data } = await this.$z.$axios.post('/ChargeCar', { gtel })
        if (data.length == 1) {
          this.$refs.from.forcesetformdatafn({ mc: data[0].mc })
          let Stationdata = await this.$z.$axios.post('/Station', {
            id: data[0].pcs,
          })
          data = Stationdata.data
          if (data.length == '1') {
            this.$refs.from.forcesetformdatafn({ pcs: data[0].pcs })
          }
        }
      },
      async delfn(datas = []) {
        datas.forEach(async (e) => {
          await this.$z.$axios.post('/ChargeCarToPcsdel', { gtel: e.gtel })
        })
        this.getdata()
      },
    },
  }
</script>
<style lang="scss" scoped>
  @import '@/styles/tablecss.scss';
</style>
